package main

import "fmt"

func main() {
	fmt.Println(longestCommonPrefix([]string{"flower", "flow", "flight"}))
}

func longestCommonPrefix(strs []string) string {
	prefix := strs[0]
	for i := 1; i < len(strs); i++ {
		prefix = lcp(prefix, strs[i])
		if len(prefix) == 0 {
			break
		}
	}
	return prefix
}

func lcp(str1, str2 string) string {
	prefix := ""
	lens := 0
	if len(str1) > len(str2) {
		lens = len(str2)
	} else {
		lens = len(str1)
	}
	for i := 0; i < lens; i++ {
		if str1[i] == str2[i] {
			prefix += string(str1[i])
		} else {
			break
		}
	}
	return prefix
}
